/**
 * $Id: RegisterAction.java 12 2010-11-02 15:33:09Z feigme $
 */
package com.iary.action.user;

import java.util.Date;

import org.apache.commons.validator.GenericValidator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iary.core.Constants;
import com.iary.entity.User;
import com.iary.service.UserService;
import com.iary.service.exception.InputExistedException;

/**
 * @author Feigme
 * 
 */
@Controller
public class RegisterAction {

	private static final Logger log = LoggerFactory
			.getLogger(RegisterAction.class);

	@Autowired
	private UserService userService;

	private Validator registerValidator;

	private String confirmPassword;

	public RegisterAction() {
		this.registerValidator = new RegisterValidator();
	}

	@RequestMapping(value = "/register", method = RequestMethod.POST)
	@ResponseBody
	public String register(@RequestParam("email") String email,
			@RequestParam("password") String password) {
		log.debug("Enter user register");
		String error = null;
		if (GenericValidator.isBlankOrNull(email)) {
			error = "E1";
		} else if (!GenericValidator.isEmail(email)) {
			error = "E2";
		}
		if (GenericValidator.isBlankOrNull(password)) {
			error = "E3";
		}
		if (error == null) {
			User user = new User();
			user.setEmail(email);
			user.setPassword(password);
			user.setCreateDate(new Date());
			user.setStatus(Constants.STATUS_VALID);
			try {
				userService.register(user);
			} catch (InputExistedException e) {
				error = "E4";
			}
		}

		return error;
	}

	class RegisterValidator implements Validator {

		public boolean supports(Class<?> clazz) {
			return clazz.equals(User.class);
		}

		public void validate(Object target, Errors errors) {
			User user = (User) target;
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
					"error.password", "Password is required.");
			ValidationUtils.rejectIfEmptyOrWhitespace(errors, "email",
					"error.email", "Email is required.");
			// if (GenericValidator.isBlankOrNull(confirmPassword)) {
			// errors.rejectValue("confirmPassword", "error.confirmPassword",
			// "Confirm Password is required.");
			// } else if (!confirmPassword.equals(user.getPassword())) {
			// errors.rejectValue("confirmPassword", "error.confirmPassword",
			// "Confirm Password must same password.");
			// }

			if (!GenericValidator.isBlankOrNull(user.getEmail())) {
				if (!GenericValidator.isEmail(user.getEmail())) {
					errors.rejectValue("email", "error.email",
							"Email format is error.");
				} else {
					User isExistEmail = userService.getUserByEmail(user
							.getEmail());
					if (isExistEmail != null)
						errors.rejectValue("email", "error.email",
								"Email has been used.");
				}
			}
		}
	}

}
